package com.rayo.accesscontrollercopy.model.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.callback.OnReaderListener;
import com.rayo.accesscontrollercopy.model.db.bean.EventData;
import com.rayo.accesscontrollercopy.model.db.dao.EventDao;
import com.rayo.accesscontrollercopy.model.db.dao.EventImpl;

import java.util.Date;
import java.util.concurrent.ExecutorService;

/**
 * Created by yanghaojie on 2017/3/14.
 */

public class ReaderService extends Service {
//    private IReaderBiz mIReaderBizInSet = new ReaderBiz();
//    private IReaderBiz mIReaderBizPlug = new ReaderBiz();
    private OfficeModeThread mOfficeModeThread = new OfficeModeThread();

    private ExecutorService mExecutorService = null;
    private Intent intent = new Intent();
    private final IBinder mBinder = new LocalBinder();

    public final static String EXTRA_DATA = "com.rayo.reader.EXTRA_DATA";

    public static final String ACTION_READER_FIND_KEY = "com.rayo.reader.findkey";
    public static final String ACTION_READER_FIND_CARD = "com.rayo.reader.findcard";
    public static final String ACTION_READER_READ_PROKEY = "com.rayo.reader.read_prokey";
    public static final String ACTION_READER_READ_USERKEY = "com.rayo.reader.read.userkey";
    public static final String ACTION_READER_READ_EVENT = "com.rayo.reader.read.event";
    public static final String ACTION_READER_KEY_OPEN = "com.rayo.reader.key.open";
    public static final String ACTION_READER_WRITE_USERKEY = "com.rayo.reader.write.userkey";
    public static final String ACTION_READER_READ_PROCARD = "com.rayo.reader.read.procard";
    public static final String ACTION_READER_READ_USERCARD = "com.rayo.reader.read.usercard";
    public static final String ACTION_READER_WRITE_USERCARD = "com.rayo.reader.write.usercard";
    public static final String ACTION_READER_CARD_OPEN = "com.rayo.reader.card.open";

    public class LocalBinder extends Binder {
        public ReaderService getService() {
            return ReaderService.this;
        }
    }

    private final OnReaderListener mOnReaderListenerInsetKey = new OnReaderListener() {
        @Override
        public <T> void readerSuccessfully(String action, T t) {
//            if (!action.isEmpty() && null != t) {
//                if (ACTION_READER_KEY_OPEN.equals(action)) {
//                    onPause(ReaderBiz.INSET_READER);
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.INSET_READER, t));
//            }
        }

        @Override
        public void readerFailed(String action, int error) {
//            if (!action.isEmpty()) {
//                if (ACTION_READER_KEY_OPEN.equals(action)) {
//                    onPause(ReaderBiz.INSET_READER);
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.INSET_READER, error));
//            }
        }
    };

    private final OnReaderListener mOnReaderListenerInsetCard = new OnReaderListener() {
        @Override
        public <T> void readerSuccessfully(String action, T t) {
//            if (!action.isEmpty() && null != t) {
//                if (ACTION_READER_CARD_OPEN.equals(action)) {
//                    onPause(ReaderBiz.INSET_READER);
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.INSET_READER, t));
//            }
        }

        @Override
        public void readerFailed(String action, int error) {
//            if (!action.isEmpty()) {
//                if (ACTION_READER_CARD_OPEN.equals(action)) {
//                    onPause(ReaderBiz.INSET_READER);
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.INSET_READER, error));
//            }
        }
    };

    private OnReaderListener mOnReaderListenerPlugKey = new OnReaderListener() {
        @Override
        public <T> void readerSuccessfully(String action, T t) {
//            if (!action.isEmpty() && null != t) {
//                if (ACTION_READER_KEY_OPEN.equals(action)) {
//                    openRelay(ReaderBiz.PLUG_IN_READER, mIReaderBizPlug.getKeyBasicInfo().getKeyId());
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.PLUG_IN_READER, t));
//            }
        }

        @Override
        public void readerFailed(String action, int error) {
//            if (!action.isEmpty()) {
//                if (ACTION_READER_KEY_OPEN.equals(action)) {
//                    openFiled(error, mIReaderBizPlug.getKeyBasicInfo().getKeyId());
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.PLUG_IN_READER, error));
//            }
        }
    };

    private OnReaderListener mOnReaderListenerPlugCard = new OnReaderListener() {
        @Override
        public <T> void readerSuccessfully(String action, T t) {
//            if (!action.isEmpty() && null != t) {
//                if (ACTION_READER_CARD_OPEN.equals(action)) {
//                    openRelay(KeyEventType.EVT_OPEN_SUCCESS_CARD2.getType(), mIReaderBizPlug.getKeyBasicInfo().getKeyId());
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.PLUG_IN_READER, t));
//            }
        }

        @Override
        public void readerFailed(String action, int error) {
//            if (!action.isEmpty()) {
//                if (ACTION_READER_CARD_OPEN.equals(action)) {
//                    openFiled(error, mIReaderBizPlug.getKeyBasicInfo().getKeyId());
//                }
//                broadcastUpdate(action, new ReaderData(ReaderBiz.PLUG_IN_READER, error));
//            }
        }
    };

    private void broadcastUpdate(String action, ReaderData readerData) {
        if (null == readerData) return;
        intent.setAction(action);
        intent.putExtra(EXTRA_DATA, readerData);
        sendBroadcast(intent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        mIReaderBizInSet = new ReaderBiz(ReaderBiz.INSET_READER, true, true);
//        mIReaderBizPlug = new ReaderBiz(ReaderBiz.PLUG_IN_READER, true, true);
//        mExecutorService = Executors.newFixedThreadPool(3);
//        mExecutorService.execute(mIReaderBizInSet.getRunnable(mOnReaderListenerInsetKey, mOnReaderListenerInsetCard));
//        mExecutorService.execute(mIReaderBizPlug.getRunnable(mOnReaderListenerPlugKey, mOnReaderListenerPlugCard));
//        mExecutorService.execute(mOfficeModeThread);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        mIReaderBizInSet.onDestroy();
//        mIReaderBizPlug.onDestroy();
//        mExecutorService.shutdown();
    }

    public void onPause(int reader) {
//        if (ReaderBiz.INSET_READER == reader) {
//            mIReaderBizInSet.onPause();
//        }
//        if (ReaderBiz.PLUG_IN_READER == reader) {
//            mIReaderBizPlug.onPause();
//        }
    }

    public void onResume(int reader) {
//        if (ReaderBiz.INSET_READER == reader) {
//            mIReaderBizInSet.onResume();
//        }
//        if (ReaderBiz.PLUG_IN_READER == reader) {
//            mIReaderBizPlug.onResume();
//        }
    }

//    public void openRelay(int event, int userid) {
//        EventDao eventDao = new EventImpl();
//        eventDao.addEvent(new EventData(GlobalVariables.getInstance().getmSiteData().getLockid(), userid,
//                event, 4, new Date()));
//        mOfficeModeThread.open();
//    }

//    public void openFiled(int event, int userid) {
//        EventDao eventDao = new EventImpl();
//        eventDao.addEvent(new EventData(GlobalVariables.getInstance().getmSiteData().getLockid(), userid,
//                event, 4, new Date()));
//    }

    public void remoteOpen() {
        mOfficeModeThread.remoteOpen();
    }
}
